﻿using AdminApi.Extension.Permission.Attributes;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace AdminApi.Extension.Permission.Model
{
    public class TreeNodeDto : ICloneable
    {
        public String Name { get; set; }
        public string Index { get; set; }
        public MenuType MenuType { get; set; }

        [JsonIgnore]
        public PermissionMenuInfo PerssionMenuInfo { get; set; }

        public String Url { get { return PerssionMenuInfo?.Url; } }
        public bool HasPermission { get; set; }
        public bool HasChilds { get; set; }
        [JsonIgnore]
        public TreeNodeDto Parent { get; set; }
        public List<TreeNodeDto> Childs { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<TreeNodeDto> Settings { get; set; }

        public object Clone()
        {
            TreeNodeDto treeNodeDto = (TreeNodeDto)this.MemberwiseClone();

            if (treeNodeDto.HasChilds)
            {
                if ((int)treeNodeDto.MenuType == 1)
                {

                    treeNodeDto.Settings = new List<TreeNodeDto>();

                    Settings.ForEach(i =>
                    {
                        var item = (TreeNodeDto)i.Clone();
                        item.Parent = treeNodeDto;
                        treeNodeDto.Settings.Add(item);
                    });
                }
                else
                {
                    treeNodeDto.Childs = new List<TreeNodeDto>();

                    Childs.ForEach(i =>
                    {
                        var item = (TreeNodeDto)i.Clone();
                        item.Parent = treeNodeDto;
                        treeNodeDto.Childs.Add(item);
                    });
                }
            }

            return treeNodeDto;
        }


        public void PickUpID(string ID)
        {
            TreeNodeDto choice = null;
            Find(this, out choice);
            Choice(choice);

            void Choice(TreeNodeDto t)
            {
                if (t != null)
                {
                    t.HasPermission = true;
                    Choice(t.Parent);
                }
            }

            bool Find(TreeNodeDto top, out TreeNodeDto outchoice)
            {
                outchoice = null;
                if (top.PerssionMenuInfo != null)
                {
                    if (top.PerssionMenuInfo.ID.Equals(ID))
                    {
                        outchoice = top;
                        return true;
                    }
                }

                if (top.HasChilds)
                {
                    if (top.Childs != null && top.Childs.Count > 0)
                    {
                        foreach (var item in top.Childs)
                        {
                            if (Find(item, out outchoice)) return true;
                        }
                    }
                    if (top.Settings != null && top.Settings.Count > 0)
                    {
                        foreach (var item in top.Settings)
                        {
                            if (Find(item, out outchoice)) return true;
                        }
                    }
                }


                return false;
            }
        }
    }
}